import { Date as XDate } from './Date'
import { cloud } from '@hw-agconnect/cloud-server'

let myHandler = async function (event, context, callback, logger) {
  try {
    logger.info(event)
    const date = new Date()
    const str = `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString()
      .padStart(2, '0')}`
    const objs = await cloud.database({ zoneName: 'Demo' })
      .collection(XDate)
      .query()
      .equalTo('date', str)
      .get()
    if (objs.length > 0) {
      callback({
        code: 0,
        data: objs[0]
      })
    } else {
      throw Error('no data')
    }
  } catch (e) {
    logger.error(e)
    callback({
      code: -1,
      data: e
    })
  }
}

export { myHandler }